05. Exercise: Notification Channels

L1 A05 Notification Channels V3

Android Developer Documentation

Exercise

  1. Open EggTimerFragment.kt and find the createChannel() function. Channels are available from api level 26.

  2. Pass the unique channel id to the constructor of NotificationChannel.

  3. Next pass the notification channel name which users will also see in their settings screen.

  4. As the last parameter, pass the importance level for the notification channel. Importance levels will be covered later in this lesson so for now you can use NotificationManager.IMPORTANCE_LOW.

  5. On the notificationChannel object set enableLights to true. This setting will enable the lights when a notification is shown.

  6. Again On the notificationChannel object set lightColor to red in order to display a red light when a notification is shown.

  7. On the notificationChannel object set enableVibration to true in order to enable vibration.

  8. On the notificationChannel object set channel description to "Time for breakfast".

private fun createChannel(channelId: String, channelName: String) {
    // TODO: Step 1.6 START create a channel
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationChannel = NotificationChannel(
            channelId,
            channelName,
            // TODO: Step 2.4 change importance
            NotificationManager.IMPORTANCE_LOW 
        )
        // TODO: Step 2.6 disable badges for this channel

        notificationChannel.enableLights(true)
        notificationChannel.lightColor = Color.RED
        notificationChannel.enableVibration(true)
        notificationChannel.description = "Time for breakfast"
  1. Get an instance of NotificationManager by calling getSystemService. Call createNotificationChannel on NotificationManager and pass notificationChannel object which you created in the previous step.
        val notificationManager = requireActivity().getSystemService(
            NotificationManager::class.java
        )
        notificationManager.createNotificationChannel(notificationChannel)


    // TODO: Step 1.6 END create channel
}
  1. Next, to create a channel, you need to call the new createChannel() extension function you just wrote. This function takes two parameters, the channel id and the channel name. You need to look up your channel id and channel name from the string resources. Find Step 1.7 in onCreateView()function of the same class and call the createChannel() function to create the channel.
// EggTimerFragment.kt
    // TODO: Step 1.7 call createChannel
    createChannel(
          getString(R.string.egg_notification_channel_id),
          getString(R.string.egg_notification_channel_name)
    )
  1. You need to pass the channel id to the notification builder. You already did this in step 1.2. Setting a wrong value as the channel id will make the notification fail. Open NotificationUtils.kt to verify the channel id you previously set is correct.
val builder = NotificationCompat.Builder(
        applicationContext,
       // TODO: Step 1.8 verify the notification channel name
        applicationContext.getString(R.string.egg_notification_channel_id)
)
  1. Run the app, you will see the app sends a notification every time you start the timer.

  2. Pull the status bar and observe the notification title, content and icon are just like you set in the previous steps. With this you started using the channel you just created and added properties like the importance. Also this allows your users to be able to control this channel separately such as turning on or off notifications from this channel.

  3. To verify the newly created channel, close the app and find the app icon. Perform a long click on the app icon and select app info.

  1. Select notification from the list of settings and you should be seeing the new channel with name Egg, right below the Show notifications setting.

By using channels, the app developers can customize the settings and behavior for all notifications sent on this channel. Your users can also change the settings for each channel by using the channel list.

Congrats, you created your first notification!